package cn.bugstack.test;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

/**
 * @description:
 * @author: wwq
 * @date: 2025/08/07/10:26
 */
@Slf4j
public class AlipayTest {

    // 「沙箱环境」应用ID - 您的APPID，收款账号既是你的APPID对应支付宝账号。获取地址；https://open.alipay.com/develop/sandbox/app
    public static String app_id = "9021000129662048";
    // 「沙箱环境」商户私钥，你的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtLJTjbL/Oz3v1sAzjHhbpLlnee9e3pOhsUcgtvesnXNnLFRs2EgJnT+XV2lvW2uFfhBV9lfrb3UQcgin3U2/r0GiZaB27a+jwk8xZMfbSm2MhsbD4o1Aip0b+91aspnklqYd2jIjFSzJ3uxmqai9wxj1ClDExF3uewn1mF5ka1m82OA1tPJ/2Mqg4s9TMTQbzWmeh8ci5Cp9hC+p7fUEVmmjbZyBGXi4vUUFu4b5Ib3MVRWQ+UGBWZT88vipw3lC2iYC4HtZf0SqskueGSdTw6sh/RUCMdBdXgdH3Amu27fpYq8vXQ2E0prurwQz63YHE/RX7eaP/ND7IJBYRFfshAgMBAAECggEAfLnZWpve4jNqUqBEB+iKE9SAgc+spOYvSKT2iot5LNahPrWq7Zy3AnX3Ubl94BexrzvQxq0ZCvYosmfFlg8QU0W9sI/eqjikg3HQAoYMAdBM692xWsM1WmAksEqwsJUOOGQL3wSFFaR9auLlL2pfVNhTEvrtGzCQpt3pt3hTm5GYR+IeLTEcBSSb1uVIggW0ok+f2A6BLKLpjlgOU5fTX0IMhpROS1rxg1zU2NNqy5MXkrZKs6ky2OpETSrpZLJJm5JOAVsKoh2B29t7s1uPJA/Y2oFUtq71c3MZqK46wrAzoKczXNIvgrW0Lvp/gwPKb3BzYIhMQrp93DS/FC02CQKBgQDxl+r3FtOBT06PrVCwRSzuMhluRB8mUGcQgjV7+8jrwM8bIIignmLozBK4lNP/ozmkgyC6W2WMMvUxDZoecWTHqial57lvDz8E0/wb9uJdLtm4TF//HdKeZGrUExi1FvGhZJ/Axw/yujlypFsbXjvvEhSQiUfAMG6KHGOwnTblvwKBgQC3gDLTi6QF8OsZA3qphtIMioS5PaNKm2F0l5uEmJjzRLueu1/a730z2K7PiyPtyrg/JyYvdOLJzPWbnwgn/D57jjQYeyebuJ1SBLqi0zwF2xXXgq9fN8pEbRrNy78WdASDeAon329srDrPMRRcYajombS2Klti3Sa9Y1r7EjQXHwKBgQCaNFSGiGxSAjPr4z04db76/sFar2KOiw1t7R6EV67kQ7ot33M1ykLpwrBEXsJ+TkoRppzyNjo35aK5P6xauBgSlZl7lc+3gqy/wSsPEmNkBO51A1LWvMtdzEyfttyzYcSzNk39t1GHpbPbZ+q7KkfpDcpQgz9n/cHgp8Q9y+tpcwKBgFmA4EmqzOBQ+HsE5WmW5xFS5xXraKBVVZ2m7SYpwEC3/8OWpVSGh80oHWwB9A5iWKgFEm0HWC8SF6JGWZYXROrDG8ntGA6uZ52/zMDqw1Yf3HF3vs50yeXCSdajUJtFD4aIoHt+UjzxbOXujH3a7KY3gptv4Y9BS0oJEjFwWZQFAoGBAKrFchcAeFxiH+k1BoPsH2hdig/phBXe6A5j+nTAVtWV5soMXlcV7QsUSt8/Vl0SGxAcyR47kdW0uw8OEgpCk4zoWF2FA8Gm9cwpoqBpBtedsK9IqBlp7aHozHvTxc9MvLTjDkRjxTXSyQhClOtF57U50iVYWdM4t001stHfjVbP";
    // 「沙箱环境」支付宝公钥
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoRUHqxzZMlNS1CO5h6Xq1Ral5wUcwP60ySbpVJSvThvE8b1fqXqopYWPZ+Hh22Wf7XdfTHd3NuGEI/M0vsqEh6DQth19uWVYSQZVvxiDDnEwL2uLSAzWNgaZJf4pn7KrZyJyvsZQSKv7/8CToQ7ruKVMGGKqIs5NWfAvQ4Jmws5NXM3ZhX+50PzntA87sieY66ORFyMyrXjF5fd/92O31R3Yk9iNlg6gIkgDbz0dQZ1tw1bJP1cNO/+bLHsKmDO3NrPNljbJMsbiPLD2gfXbRvtLX00L4dU30tgkt3iS/9H1sj0wEWIDF0xpo4y/ijeUCVrlmR7WF9l9ENxXRLfWJwIDAQAB";
    // 「沙箱环境」服务器异步通知回调地址
    public static String notify_url = "https://xfg.natapp.cn/api/v1/alipay/alipay_notify_url";
    // 「沙箱环境」页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "https://gaga.plus";
    // 「沙箱环境」
    public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 签名方式
    public static String sign_type = "RSA2";
    // 字符编码格式
    public static String charset = "utf-8";

    private AlipayClient alipayClient;

    @Before
    public void init() {
        this.alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id,
                merchant_private_key,
                "json",
                charset,
                alipay_public_key,
                sign_type);
    }

    @Test
    public void test_aliPay_pageExecute() throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); // 发送请求的 Request类
        request.setNotifyUrl(notify_url);
        request.setReturnUrl(return_url);

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "xfg000091004001");
        bizContent.put("total_amount", "0.01"); // 订单的总金额
        bizContent.put("subject", "测试商品");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        request.setBizContent(bizContent.toString());

        String form = alipayClient.pageExecute(request).getBody();
        log.info("测试结果：{}", form);

    }

    /**
     * 查询订单
     */
    @Test
    public void test_alipay_certificateExecute() throws AlipayApiException {
        AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
        bizModel.setOutTradeNo("daniel821231231231");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(bizModel);

        String body = alipayClient.execute(request).getBody();
        log.info("测试结果:{}", body);
    }

    /**
     * 退款接口
     */
    @Test
    public void test_alipay_refund() throws AlipayApiException {
        AlipayTradeRefundRequest request =new AlipayTradeRefundRequest();
        AlipayTradeRefundModel refundModel =new AlipayTradeRefundModel();
        refundModel.setOutTradeNo("daniel82AAAA000032333361X03");
        refundModel.setRefundAmount("1.00");
        refundModel.setRefundReason("退款说明");
        request.setBizModel(refundModel);

        AlipayTradeRefundResponse execute = alipayClient.execute(request);
        log.info("测试结果：{}", execute.isSuccess());
    }

}
